package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportPurchaseInventoryRpcService;
import com.hunttown.mes.rpc.domain.TransportPurchaseInventoryDTO;
import com.hunttown.mes.rpc.domain.query.TransportPurchaseInventoryDTOQuery;
import com.hunttown.mes.service.TransportPurchaseInventoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportPurchaseInventoryConverts.convert;

/**
 * created by wangjunfu on 2019-12-24 15:52:00
 */
@Service
public class TransportPurchaseInventoryRpcServiceImpl implements TransportPurchaseInventoryRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportPurchaseInventoryRpcServiceImpl.class);

    private final TransportPurchaseInventoryService transportPurchaseInventoryService;

    @Autowired
    public TransportPurchaseInventoryRpcServiceImpl(TransportPurchaseInventoryService transportPurchaseInventoryService) {
        this.transportPurchaseInventoryService = transportPurchaseInventoryService;
    }

    @Override
    public TransportPurchaseInventoryDTO insert(TransportPurchaseInventoryDTO transportPurchaseInventoryDTO) {
        try {
            return convert(transportPurchaseInventoryService.insert(convert(transportPurchaseInventoryDTO)));
        } catch (Exception e) {
            logger.error("TransportPurchaseInventory create failed.", e);
            throw new RuntimeException("TransportPurchaseInventory create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportPurchaseInventoryDTO transportPurchaseInventoryDTO) {
        try {
            return transportPurchaseInventoryService.updateInfoById(convert(transportPurchaseInventoryDTO));
        } catch (Exception e) {
            logger.error("TransportPurchaseInventory update by id failed.", e);
            throw new RuntimeException("TransportPurchaseInventory update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportPurchaseInventoryDTOQuery transportPurchaseInventoryDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseInventoryDTOQuery.getW_id() != null && transportPurchaseInventoryDTOQuery.getW_id() > 0) {
                query.put("w_id", transportPurchaseInventoryDTOQuery.getW_id());
            }

            if (transportPurchaseInventoryDTOQuery.getIds() != null && !transportPurchaseInventoryDTOQuery.getIds().equals("")) {
                query.put("ids", transportPurchaseInventoryDTOQuery.getIds());
            }

            if (transportPurchaseInventoryDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseInventoryDTOQuery.getStopFlag());
            }

            if (transportPurchaseInventoryDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseInventoryDTOQuery.getDeleteFlag());
            }

            return transportPurchaseInventoryService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportPurchaseInventory update by query failed.", e);
            throw new RuntimeException("TransportPurchaseInventory update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseInventoryDTO selectById(Integer id) {
        try {
            return convert(transportPurchaseInventoryService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportPurchaseInventory select by id failed.", e);
            throw new RuntimeException("TransportPurchaseInventory select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseInventoryDTO selectObjByQuery(TransportPurchaseInventoryDTOQuery transportPurchaseInventoryDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseInventoryDTOQuery.getPurchaseType() != null) {
                query.put("purchaseType", transportPurchaseInventoryDTOQuery.getPurchaseType());
            }

            if (transportPurchaseInventoryDTOQuery.getpYear() != null) {
                query.put("pYear", transportPurchaseInventoryDTOQuery.getpYear());
            }

            if (transportPurchaseInventoryDTOQuery.getpMonth() != null) {
                query.put("pMonth", transportPurchaseInventoryDTOQuery.getpMonth());
            }

            if (transportPurchaseInventoryDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseInventoryDTOQuery.getStopFlag());
            }

            if (transportPurchaseInventoryDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseInventoryDTOQuery.getDeleteFlag());
            }

            return convert(transportPurchaseInventoryService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportPurchaseInventory select obj by query failed.", e);
            throw new RuntimeException("TransportPurchaseInventory select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportPurchaseInventoryDTO> getListForPage(TransportPurchaseInventoryDTOQuery transportPurchaseInventoryDTOQuery) {
        Query query = Query.page(transportPurchaseInventoryDTOQuery.getPn(), transportPurchaseInventoryDTOQuery.getPageSize());

        if (transportPurchaseInventoryDTOQuery.getId() != null && transportPurchaseInventoryDTOQuery.getId() > 0) {
            query.put("id", transportPurchaseInventoryDTOQuery.getId());
        }

        if (transportPurchaseInventoryDTOQuery.getIds() != null && !transportPurchaseInventoryDTOQuery.getIds().equals("")) {
            query.put("ids", transportPurchaseInventoryDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportPurchaseInventoryDTOQuery.getLikeName())) {
            query.put("likeName", transportPurchaseInventoryDTOQuery.getLikeName());
        }

        if (transportPurchaseInventoryDTOQuery.getPurchaseType() != null) {
            query.put("purchaseType", transportPurchaseInventoryDTOQuery.getPurchaseType());
        }

        if (transportPurchaseInventoryDTOQuery.getpYear() != null) {
            query.put("pYear", transportPurchaseInventoryDTOQuery.getpYear());
        }

        if (transportPurchaseInventoryDTOQuery.getpMonth() != null) {
            query.put("pMonth", transportPurchaseInventoryDTOQuery.getpMonth());
        }

        if (StringUtils.isNotBlank(transportPurchaseInventoryDTOQuery.getShowDate())) {
            query.put("showDate", transportPurchaseInventoryDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseInventoryDTOQuery.getStartDate())) {
            query.put("startDate", transportPurchaseInventoryDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseInventoryDTOQuery.getEndDate())) {
            query.put("endDate", transportPurchaseInventoryDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseInventoryDTOQuery.getOrderBy())) {
            query.put("orderBy", transportPurchaseInventoryDTOQuery.getOrderBy());
        }

        if (transportPurchaseInventoryDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportPurchaseInventoryDTOQuery.getStopFlag());
        }

        if (transportPurchaseInventoryDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportPurchaseInventoryDTOQuery.getDeleteFlag());
        }

        List<TransportPurchaseInventoryDTO> list = convert(transportPurchaseInventoryService.selectByQuery(query));
        Page<TransportPurchaseInventoryDTO> page = Page.from(transportPurchaseInventoryDTOQuery.getPn(), transportPurchaseInventoryDTOQuery.getPageSize(), list);
        page.setTotalItems(transportPurchaseInventoryService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportPurchaseInventoryService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportPurchaseInventory delete failed.", e);
            throw new RuntimeException("TransportPurchaseInventory delete failed, Please order your code.", e);
        }
    }
}